package advance.day07;


/**
 * 描述：
 *      换钱的方法数
 * @author hl
 * @version 1.0
 * @date 2020/11/13 15:33
 */
public abstract class CoinsWay {

    public static int process(int[] arr, int index, int aim){
        if (index == arr.length) {
            return aim == 0 ? 1 : 0;
        }
        int res = 0;
        for (int i = 0; arr[index] * i <= aim; i++) {
            res += process(arr, index + 1, aim - arr[index] * i);
        }
        return res;
    }

    public static int dp(int[] arr, int aim){
        int[][] dp = new int[arr.length + 1][aim + 1];
        dp[arr.length][0] = 1;
        for (int i = arr.length - 1; i >= 0; i--) {
            for (int j = 0; j <= aim; j++) {
                if (j - arr[i] >= 0) {
                    dp[i][j] = dp[i + 1][j] + dp[i][j - arr[i]];
                }else{
                    dp[i][j] = dp[i + 1][j];
                }
            }
        }
        return dp[0][aim];
    }

    public static int dp2(int[] arr, int aim){
        int[] dp = new int[aim + 1];
        dp[0] = 1;
        for (int i = arr.length - 1; i >= 0; i--) {
            for (int j = 0; j <= aim; j++) {
                if (j - arr[i] >= 0) {
                    dp[j] = dp[j] + dp[j - arr[i]];
                }
            }
        }
        return dp[aim];
    }

    public static void main(String[] args) {
        int[] arr = {5, 10, 25, 1};
//        int[] arr = {5, 3};
//        System.out.println(process(arr, 0, 2));
        System.out.println(dp(arr, 15));
        System.out.println(dp2(arr, 15));
    }
}
